define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        user_models = require('js/apps/console/models'),
        utils = require('js/utils/tools'),
        tz = require('js/utils/tz'),
        app = Backbone.history;
    require('datetimepicker');
    require('multiselect');
    require('magnific');

    var WithdrawAccountView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #export': 'exportData'
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.WithdrawAccountCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function () {
            notify.warning('警告', '导出中，请等待');
            utils.exportData(this.collection.url, this._getSearchOption());
        },
        _getSearchOption: function () {
            var options = {};
            var searched = utils.getAllInput('#sidebar_right');
            searched.user_id = $('#searchUid').val();
            searched.no = $('#searchNo').val();
            searched.name = $('#searchName').val();
            searched.target_type = parseInt($('#searchType').val());
            if (searched['target_type'] != -1) {
                options['type'] = searched['target_type'];
            }
            if (searched.user_id) {
              options.user_id = searched.user_id;
            }
            if (searched.no) {
              options.no = searched.no;
            }
            if (searched.name) {
              options.name = {};
              options.name.$like = searched.name;
            }
            utils.saveSearched('op/withdraw/account', searched);
            return options;
        },
        doSearch: function () {
            var options = this._getSearchOption();
            options.$page = 1;
            options.$size = 300;
            options._ = new Date().getTime();
            app.navigate(utils.composeQueryString('#op/withdraw_account/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('op/withdraw/account')
            }));
            var user_dict = {};
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'count',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/withdraw_account/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.getPaginator(that.options, this.collection.total, '#op/withdraw_account/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/WithdrawAccount.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            this.options.$size = 300;
            var view = new WithdrawAccountView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});
